/*

Server Side Validation Library: A C# library for validation.
Copyright (C) 2006  Chad Finsterwald, CoreWeb

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace CoreWebLibrary.Validation.Windows
{
    public class BrokenWebRules : BrokenRules
    {
        public const string DEFAULT_MIN_LENGTH_DESCRIPTION = "The field does not meet minimum length requirements";
        public const string DEFAULT_MAX_LENGTH_DESCRIPTION = "The field exceeds the maximum length allowed.";

        #region Properties 
        
        private string m_minLengthDescription = DEFAULT_MIN_LENGTH_DESCRIPTION;
        public string MinimumLengthDescription
        {
            get { return m_minLengthDescription; }
            set { m_minLengthDescription = value; }
        }

        private string m_maxLengthDescription = DEFAULT_MAX_LENGTH_DESCRIPTION;
        public string MaximumLengthDescription
        {
            get { return m_maxLengthDescription; }
            set { m_maxLengthDescription = value; }
        }

        private BrokenRulesStyleCollection m_styles = null;
        public BrokenRulesStyleCollection Styles
        {
            get { return m_styles; }
            set { m_styles = value ; }
        }

        #endregion

        public BrokenWebRules(BrokenRulesStyleCollection styles)
        {
            m_styles = styles;
        }

        public virtual bool Required(string valueToCheck,
            string name,
            string description,
            params Control[] controls)
        {
            return Assert(valueToCheck.Length != 0, valueToCheck, int.MinValue, int.MaxValue, name, description, controls);
        }

        public virtual bool Assert(bool assertion,
            string name,
            string description,
            params Control[] controls)
        {
            return Assert(assertion, string.Empty, int.MinValue, int.MaxValue, name, description, controls);
        }

        public virtual bool Assert(bool assertion,
             string valueToCheck,
             int minLength,
             int maxLength,
             string name,
             string description,
             params Control[] controls)
        {
            bool isValidAssertion = true;
            string descrip = string.Empty;

            if (valueToCheck.Length < minLength)
            {
                descrip = MinimumLengthDescription;
                isValidAssertion = false;
            }
            else if (valueToCheck.Length > maxLength)
            {
                descrip = MaximumLengthDescription;
                isValidAssertion = false;
            }
            else if (assertion == false)
            {
                descrip = description;
                isValidAssertion = false;
            }

            if (isValidAssertion == false)
            {
                Add(name, descrip);
            }

            DoModifyControls(isValidAssertion, descrip, controls);

            return isValidAssertion;
        }


        protected virtual void DoModifyControls(bool isValidAssertion,
            string description,
            Control[] controls)
        {
            foreach (StyleItem si in Styles.Values)
            {
                si.ApplyStyle(isValidAssertion, description, controls);
            }
        }
    }
}
